﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;

namespace MS.Interface
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private Border _activeBorder;
        private int _timeModeling;
        private readonly SourceContent _sourceContent;


        public MainWindow()
        {
            InitializeComponent();
            _sourceContent = (SourceContent) Activator.CreateInstance(typeof(SourceContent));
            SourseContentControl.Content = _sourceContent;
            _activeBorder = SourseMenuItemBorder;

            
        }


        private void OnMenuItemMouseDown(object sender, MouseButtonEventArgs e)
        {
            _activeBorder.Background = Brushes.White;
            _activeBorder = (Border) sender;
            _activeBorder.Background = Brushes.AliceBlue;
        }

        private Timer _stateTimer;

        private void ProcessButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {


            _timeModeling  = Convert.ToInt32(_sourceContent.TimeModeling.Text);
            ProcessProgressBar.Maximum = _timeModeling;
            ProcessProgressBar.Minimum = 0;
            ProcessProgressBar.Value = 0;
            var autoEvent = new AutoResetEvent(true);
            TimerCallback tcb = UpdateProgressBar;
            _stateTimer = new Timer(tcb, autoEvent, 0, 1000);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.ToString());
            }
        }


        private void UpdateProgressBar(object state)
        {
            try
            {
                if (ProcessProgressBar != null)
                {
                    var theProcessProgressBar = ProcessProgressBar;

                    {
                        // Checking if this thread has access to the object.
                        if (theProcessProgressBar.Dispatcher.CheckAccess())
                        {
                            if (ProcessProgressBar.Value == _timeModeling)
                            {
                                _stateTimer.Dispose();
                            }
                            // This thread has access so it can update the UI thread.      
                            theProcessProgressBar.Value +=1 ;
                        }
                        else
                        {
                            // This thread does not have access to the UI thread.
                            // Place the update method on the Dispatcher of the UI thread.
                            theProcessProgressBar.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                                                         (Action)(() => { theProcessProgressBar.Value += 1; }));
                        }
                    }
                }
                //ProcessProgressBar.Value =ProcessProgressBar.Value + 1;

            }
            catch (Exception ex)
            {
                
                throw new Exception(ex.ToString());
            }

        }

       
        
    }
}
